package cy.mybatis.genui;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.swing.table.AbstractTableModel;


public class CheckTableModel extends AbstractTableModel {

    private static final String ERROR_DATA_LENGTH = "数据长度错误";
    protected int checkColumn = -1;

    protected List <String> columnNames;
    protected List <Object> contents;

    public CheckTableModel() {
        this.columnNames = new ArrayList<String>();
        this.contents = new ArrayList<Object>();
    }

    public CheckTableModel(String[] columnNames) {
        this();

        if (null == columnNames) {
            return;
        }
        this.columnNames.addAll(Arrays.asList(columnNames));
    }
    public CheckTableModel(Object[][] datas, String[] columnNames) throws Exception {
        this(columnNames);
        refreshContents(datas);
    }

    public void refreshContents(Object[][] datas) throws Exception {
        this.contents.clear();
        if (null == datas) {
            return;
        }

        for (Object[] data : datas) {
            addRow(data);
        }
    }

    public void addRow(Object[] data) throws Exception {
        if (null == data) {
            return;
        }

        if (this.columnNames.size() != data.length) {
            throw new Exception(ERROR_DATA_LENGTH);
        }

        List <Object> content = new ArrayList<Object>(this.columnNames.size());

        for (int ii = 0; ii < this.columnNames.size(); ii++) {
            content.add(data[ii]);
        }
        contents.add(content);
    }

    public void removeRow(int row) {
        contents.remove(row);
    }

    public void removeRows(int row, int count) {
        for (int ii = 0; ii < count; ii++) {
            if (contents.size() > row) {
                contents.remove(row);
            }
        }
    }

    @Override
    public boolean isCellEditable(int row, int col) {
        if (col == this.checkColumn) {
            return true;
        }

        return super.isCellEditable(row, col);
    }

    @SuppressWarnings({"rawtypes", "unchecked"})
    @Override
    public void setValueAt(Object value, int row, int col) {
        ((List) contents.get(row)).set(col, value);
        this.fireTableCellUpdated(row, col);
    }
    @Override
    public Class <?> getColumnClass(int column) {
        Object value = getValueAt(0, column);

        if (value != null) {
            return value.getClass();
        }

        return super.getClass();
    }

    @Override
    public int getColumnCount() {
        return this.columnNames.size();
    }

    @Override
    public int getRowCount() {
        return this.contents.size();
    }

    @SuppressWarnings("rawtypes")
    @Override
    public Object getValueAt(int row, int col) {
        return ((List) contents.get(row)).get(col);
    }

    @Override
    public String getColumnName(int col) {
        return columnNames.get(col);
    }

    public int getCheckColumn() {
        return checkColumn;
    }
    public void setCheckColumn(int checkColumn) {
        this.checkColumn = checkColumn;
    }
}
